package core.Resource
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import util.PathHelper;

	public class DressPackManager extends EventDispatcher
	{
		private var _dressPacks:Array;
		
		public function DressPackManager()
		{
			_dressPacks = [];
		}
		
		public static function get instance():DressPackManager
		{
			if(_instance == null)
				_instance = new DressPackManager();
			return _instance;
		}
		
		public function getDress(name:String):DressPack
		{
			var pack:DressPack;
			if(_dressPacks[name] !=null)
			{
				pack = _dressPacks[name] as DressPack;
				if(pack.state!=1)
				{
					pack.addEventListener(Event.COMPLETE,completeHandler);
				}
				else
				{
					this.dispatchEvent(new DressPackEvent(DressPackEvent.COMPLETE,pack));
				}
			}
			else
			{
				//TODO:如果没读完，先返一个默认样式
				pack = InstancePool.instance.getDressPack();
				pack.addEventListener(Event.COMPLETE,completeHandler);
				pack.load(name,PathHelper.getDressPath(name));
				_dressPacks[name] = pack;
			}
			return pack; 
		}
		
		public function clearDress(name:String):void
		{
			if(_dressPacks[name] !=null)
			{
				InstancePool.instance.setDressPack(_dressPacks[name]);
				delete _dressPacks[name];
			}
		}
		
		private function completeHandler(e:Event):void
		{
			var pack:DressPack = e.target as DressPack;
			pack.removeEventListener(Event.COMPLETE,completeHandler);
			this.dispatchEvent(new DressPackEvent(DressPackEvent.COMPLETE,pack));
		}
		
		private static var _instance:DressPackManager;
		
	}
}